﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Web.Configuration;
using System.Diagnostics;

namespace Eugenhome.Atrium.Mail.Web
{
    /// <summary>
    /// 
    /// </summary>
    public partial class MailView : System.Web.UI.Page
    {
        /// <summary></summary>
        protected GadagetMessageCollection _gmc { get { return (GadagetMessageCollection)Session["GadagetMessageCollection"]; } }

        /// <summary></summary>
        private Login _login = new Login();

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            _login.Check();

            string mailMethod = WebConfigurationManager.AppSettings[DAO.WEBCONFIG_MAILMETHOD_KEY] != null ? WebConfigurationManager.AppSettings[DAO.WEBCONFIG_MAILMETHOD_KEY] : DAO.WEBCONFIG_MAILMETHOD_VALUE_ATOM;

            string user = "eugen.dolgi@gmail.com";
            string pass = _login.DecryptedUserKey;

            if (mailMethod.Equals(DAO.WEBCONFIG_MAILMETHOD_VALUE_ATOM))
            {
                if (_gmc == null)
                {
                    Session["GadagetMessageCollection"] = new GadagetMessageCollection();

                    string proxyAddress = WebConfigurationManager.AppSettings[DAO.WEBCONFIG_HOSTINGPROXY_KEY] != null ? WebConfigurationManager.AppSettings[DAO.WEBCONFIG_HOSTINGPROXY_KEY] : "";

                    _gmc.SetAtomMode(user, pass, proxyAddress);
                }
            }
            else if (mailMethod.Equals(DAO.WEBCONFIG_MAILMETHOD_VALUE_IMAP))
            {
                if (_gmc == null)
                {
                    Session["GadagetMessageCollection"] = new GadagetMessageCollection();
                    _gmc.CacheFullFileName = Server.MapPath("mail.xml");
                    _gmc.SetImapMode("imap.gmail.com", 993, true, user, pass);
                }
                else
                {
                    string reload = Request["reload"];
                    if (reload != null && !reload.Equals(string.Empty))
                    {
                        _gmc.Reset(ResetActions.CacheFile);
                        Session["GadagetMessageCollection"] = new GadagetMessageCollection();
                        _gmc.CacheFullFileName = Server.MapPath("mail.xml");
                        _gmc.SetImapMode("imap.gmail.com", 993, true, "eugen.dolgi@gmail.com", pass);
                    }
                }

                //Debug-Modus
                //_gmc.OnlineOn = false;

            }

            repMails.DataSource = _gmc.Update().Select(item => new MailItem(item)).ToList();
            repMails.DataBind();

            string errorStr = "";
            _gmc.Errors.ForEach(item => errorStr += item + "; ");

            imgReload.ImageUrl = _gmc.Connected ? DAO.IMAGE_CONNECTED : DAO.IMAGE_DISCONNECTED;
            imgReload.ToolTip = errorStr;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            ImageButton cb = (ImageButton)sender;
            //string id = cb.CommandArgument;
            //_gmc.DeleteFromServer(id);

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDeleteSelection_Click(object sender, ImageClickEventArgs e)
        {
            List<string> mailToDel = Request.Form.AllKeys.Where(item => item.StartsWith("cbmail")).ToList();
            if (!mailToDel.Count.Equals(0))
            {
                mailToDel.Select(item => item.Replace("cbmail", "")).ToList().ForEach(item => _gmc.DeleteFromServer(item));

                repMails.DataSource = _gmc.Update().Select(item => new MailItem(item)).ToList();
                repMails.DataBind();

                imgReload.ImageUrl = _gmc.Connected ? DAO.IMAGE_CONNECTED : DAO.IMAGE_DISCONNECTED;
            }
        }
    }
}